﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_nhà_cung_cấp.Quản_lý_thông_tin_nhà_cung_cấp
{
    public partial class FormEditNCC : Form
    {
        CXulyNhacungcap xulyNCC;
        Quản_lý_sách.Quản_lý_thông_tin_sách.CXulythongtinsach xulySach;
        Kết_nối.NhaCungCap NCC=null;
        List<Kết_nối.Sach> dsSach;
        List<Kết_nối.Sach> danhmucsach;
        
        public FormEditNCC(int button,string mancc)
        {
            InitializeComponent();
            xulyNCC = new CXulyNhacungcap();
            if (button == 0)
            {
                btnchung.Text = "Add";
                NCC = new Kết_nối.NhaCungCap();
            }
            else
            {
                btnchung.Text = "Save";
                NCC = xulyNCC.TimNhacungcapma(mancc);
            }
        }

        private void FormEditNCC_Load(object sender, EventArgs e)
        {
            xulySach = new Quản_lý_sách.Quản_lý_thông_tin_sách.CXulythongtinsach();
            dsSach = new List<Kết_nối.Sach>();
            danhmucsach = new List<Kết_nối.Sach>();
            dsSach = xulySach.getdsSach();
            
            if (btnchung.Text == "Save")
            {
                if (NCC != null && NCC.Nhacungcapstatus == "live")
                {
                    txtten.Text = NCC.NhaCungCapName;
                    txtdiachi.Text = NCC.NhaCungCapAddress;
                    txtsdt.Text = NCC.NhaCungCapPhoneNumber;
                    txtemail.Text = NCC.NhaCungCapEmail;
                    txtdes.Text = NCC.NhaCungCapDescription;

                    foreach (Kết_nối.Sach_NhaCungCap sncc in NCC.Sach_NhaCungCaps)
                    {
                        danhmucsach.Add(sncc.Sach);
                        foreach (Kết_nối.Sach sach in dsSach)
                        {
                            if (sach.SachNo == sncc.Sach.SachNo)
                            {
                                dsSach.Remove(sach);
                                break;
                            }
                        }
                        //dsSach.Remove(dsSach.Where(x=>x.SachNo==sncc.Sach.SachNo).First());
                    }
                    HienThiDanhMucSach(danhmucsach);
                    hienthidsSach(dsSach);
                }
            }
            else
            {
                hienthidsSach(xulySach.getdsSach());
            }
        }

        private void btnchung_Click(object sender, EventArgs e)
        {
            if (btnchung.Text == "Add")
            {
                NCC.NhaCungCapID = xulyNCC.GetdsNhaCungCap().Last().NhaCungCapID + 1;
                NCC.NhaCungCapNo = string.Concat("NCC", NCC.NhaCungCapID.ToString());
                NCC.NhaCungCapName = txtten.Text;
                NCC.NhaCungCapAddress = txtdiachi.Text;
                NCC.NhaCungCapPhoneNumber = txtsdt.Text;
                NCC.NhaCungCapEmail = txtemail.Text;
                NCC.NhaCungCapDescription = txtdes.Text;
                NCC.Nhacungcapstatus = "live";
                xulyNCC.ThemNhacungcap(NCC);
                Close();
            }
            else
            {
                Kết_nối.NhaCungCap ncc = new Kết_nối.NhaCungCap();
                ncc.NhaCungCapID = NCC.NhaCungCapID;
                ncc.NhaCungCapNo = NCC.NhaCungCapName;
                ncc.NhaCungCapName = txtten.Text;
                ncc.NhaCungCapAddress = txtdiachi.Text;
                ncc.NhaCungCapPhoneNumber = txtsdt.Text;
                ncc.NhaCungCapEmail = txtemail.Text;
                ncc.NhaCungCapDescription = txtdes.Text;
                xulyNCC.SuaNhacungcap(ncc);
                Close();
            }
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            Close();
        }

        //hien thi sach 
        private void hienthidsSach(List<Kết_nối.Sach> ds_sach)
        {
            lvSach.Items.Clear();
            foreach (Kết_nối.Sach sach in ds_sach)
            {
                if (sach.SachStatus == "live")
                {
                    ListViewItem lvi = lvSach.Items.Add(sach.SachNo);
                    lvi.SubItems.Add(sach.SachName);
                }
            }
        }

        //hien thi danh muc sach
        private void HienThiDanhMucSach(List<Kết_nối.Sach> dsSach)
        {
            lvDanhMucSach.Items.Clear();
            foreach (Kết_nối.Sach s in dsSach)
            {
                if (s.SachStatus == "live")
                {
                    ListViewItem lvi = lvDanhMucSach.Items.Add(s.SachNo);
                    lvi.SubItems.Add(s.SachName);
                }
            }
        }

        private void btnAdd_all_Click(object sender, EventArgs e)
        {

        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem liv in lvSach.SelectedItems)
            {
                string ma = liv.SubItems[0].Text;
                danhmucsach.Add(xulySach.TimSachtheoma(ma));
                foreach (Kết_nối.Sach sach in dsSach)
                {
                    if (sach.SachNo == ma)
                    {
                        dsSach.Remove(sach);
                        break;
                    }
                }
                xulyNCC.themDanhMucSach(NCC ,ma);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {

        }

        private void btnRemove_all_Click(object sender, EventArgs e)
        {

        }

        

        
    }
}
